package com.mk.admin.service.impl;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.mk.admin.mapper.PayOrderMapper;
import com.mk.admin.mapper.SelfBillingMapper;
import com.mk.admin.model.ResultDto;
import com.mk.admin.mapper.PrescriptionMapper;
import com.mk.admin.model.dto.PayOrderDto;
import com.mk.admin.model.dto.PrescriptionNoPayDto;
import com.mk.admin.model.dto.SelfBillingDto;
import com.mk.admin.model.entity.PayOrderEntity;
import com.mk.admin.model.entity.SelfBillingEntity;
import com.mk.admin.model.query.PrescriptionNoPayDetailQuery;
import com.mk.admin.model.query.PrescriptionNoPayQuery;
import com.mk.admin.model.query.PrescriptionStatusQuery;
import com.mk.admin.model.vo.*;
import com.mk.admin.service.PrescriptionService;
import com.mk.admin.model.entity.PrescriptionEntity;
import com.mk.admin.service.SelfBillingService;
import com.mk.admin.util.DateTool;
import com.mk.admin.util.HttpTool;
import com.mk.admin.util.JSONCopy;
import com.mk.admin.util.NumberTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class PrescriptionServiceImpl implements PrescriptionService {
    @Autowired
    private PrescriptionMapper prescriptionMapper;
    @Autowired
    private SelfBillingMapper selfBillingMapper;
    @Autowired
    private PayOrderMapper payOrderMapper;

    @Override
    public ResultDto selectHadPay(PrescriptionHadPayVo vo) {
        JSONObject jsonObject = selectHadPayJson(vo);
        Object result = jsonObject.get("result");
//        if (result == null) {
//            return ResultDto.data(new ArrayList<>());
//        }
//        if ("[]".equals(result.toString())) {
//            return ResultDto.data(new ArrayList<>());
//        }
//        List<PrescriptionCompletedDto> list = JSONArray.parseArray(result.toString(), PrescriptionCompletedDto.class);
        return ResultDto.data(result);
    }

    @Override
    public JSONObject selectHadPayJson(PrescriptionHadPayVo vo) {
        return HttpTool.postJson("opt_0011", JSONObject.toJSONString(vo));
    }

    @Override
    public ResultDto selectHadPayDetail(PrescriptionHadPayDetailVo vo) {
        JSONObject jsonObject = HttpTool.postJson("opt_0012", vo);
        Object result = jsonObject.get("result");
        return ResultDto.data(result);
    }

    @Override
    public ResultDto selectHadPayBySerialNo(PrescriptionPaySerialNoVo vo) {
        return null;
    }

    @Override
    public ResultDto getPrescByDocId(PrescriptionVo vo) {
        PrescriptionEntity entity = JSONCopy.copy(vo, PrescriptionEntity.class);
        return ResultDto.data(prescriptionMapper.count(entity), prescriptionMapper.select(entity));
    }

    @Override
    public ResultDto selectNoPay(PrescriptionNoPayQuery query) {
        JSONObject jsonObject = HttpTool.postJson("opt_0014", query);
        Object result = jsonObject.get("result");
        if (result != null) {
            List<PrescriptionNoPayDto> list = JSONArray.parseArray(result.toString(), PrescriptionNoPayDto.class);
            for (PrescriptionNoPayDto dto : list
            ) {
                SelfBillingEntity entity = new SelfBillingEntity();
                entity.setPreId(dto.getPreId());
                entity.setPatId(dto.getPatId());
//        将得到的值和自助开单里面的处方id进行比较，如果有，则是自助开单，否则是医嘱处方
                List<SelfBillingDto> dtoList = selfBillingMapper.select(entity);
//                自助开单在点击支付时才生成订单，医生开单
                if (dtoList.size() > 0) {
                    dto.setIsSelfBilling(1);
                    dto.setName(dtoList.get(0).getName());
                    dto.setDepartmentName(dtoList.get(0).getDepartmentName());
                    dto.setExecDeptName(dtoList.get(0).getExecDeptName());
                } else {
//                    医生开单门诊处方
                    dto.setIsSelfBilling(0);
//                    医生开单，这里直接生成一个订单到订单表
                    PayOrderEntity payOrderEntity = new PayOrderEntity();
                    payOrderEntity.setRegId(dto.getPreId());
                    payOrderEntity.setPatId(dto.getPatId());
//                    去订单表查询是否有当前数据，如果有，则不管，没有，则新增
                    List<PayOrderDto> payOrderDtoList = payOrderMapper.select(payOrderEntity);
                    if (payOrderDtoList.size() == 0) {
                        payOrderEntity.setPayStatus(1);
                        payOrderEntity.setPayStatusName("待支付");
                        payOrderEntity.setPayMoney(NumberTool.strToMoney(dto.getPayMoney()));
                        payOrderEntity.setCreateTime(DateTool.format());
                        payOrderEntity.setDepartmentName(dto.getDepartmentName());
                        payOrderEntity.setDoctorCode(dto.getDoctorCode());
                        payOrderEntity.setDoctorName(dto.getDoctorName());
                        payOrderEntity.setStatus(1);
                        payOrderEntity.setPaySourceType(1);
                        payOrderEntity.setPaySourceName("门诊缴费");
                        payOrderMapper.insert(payOrderEntity);
                    }
                }
            }
            return ResultDto.data(list);
        }
        return ResultDto.data(jsonObject);
    }

    @Override
    public ResultDto selectNoPayDetail(PrescriptionNoPayDetailQuery query) {
        JSONObject jsonObject = HttpTool.postJson("opt_0015", query);
        Object result = jsonObject.get("result");
        return ResultDto.data(result);
    }

    @Override
    public ResultDto prescribe(PrescriptionAddBillingVo vo) {
        JSONObject jsonObject = HttpTool.postJson("opt_0056", vo);
        Object result = jsonObject.get("result");
        return ResultDto.data(result);
    }

    @Override
    public ResultDto examine(PrescriptionExamineVo vo) {
        JSONObject jsonObject = HttpTool.postJson("opt_0057", vo);
        Object result = jsonObject.get("result");
        return ResultDto.data(result);
    }

    @Override
    public ResultDto cancelBilling(PrescriptionCancelBillingVo vo) {
        JSONObject jsonObject = HttpTool.postJson("opt_0058", vo);
        Object result = jsonObject.get("result");
        return ResultDto.data(result);
    }

    @Override
    public ResultDto statusQuery(PrescriptionStatusQuery query) {
        JSONObject jsonObject = HttpTool.postJson("opt_0059", query);
        Object result = jsonObject.get("result");
        return ResultDto.data(result);
    }
}
